﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tanmia.Basis.Core;
using Tanmia.Basis.Data;
using Tanmia.Basis.Data.NHibernate;

namespace Tanmia.Basis.Domain
{
    /// <summary>
    /// Represent the base class for all domain objects of the business application.
    /// </summary>
    /// <typeparam name="T">BusinessObject which is going to be manipulated through this DomainObject.</typeparam>
    public abstract class BaseDomainObject<T>
          where T : BaseBusinessObject
    {
        protected readonly IRepository<T> repository;
        private readonly ISessionManager sessionManager;

        public BaseDomainObject(ISessionManager sessionManager)
        {
            this.sessionManager = sessionManager;
            this.repository = new Repository<T>(sessionManager);
        }

        /// <summary>Adds or Updates business entity object against the database.</summary>
        /// <param name="entity"></param>
        /// <exception cref="System.ArgumentNullException">Throw when the parameter is null.</exception>
        public virtual void Save(T entity)
        {
            if (entity == null)
                throw new ArgumentNullException("entity");

            repository.Save(entity);
        }

        /// <summary>Delete business entity object from the database.</summary>
        /// <exception cref="System.ArgumentNullException">Throw when the parameter is null.</exception>
        public virtual void Delete(T entity)
        {
            if (entity == null)
                throw new ArgumentNullException("entity");

            repository.Delete(entity);
        }

        /// <summary>Return the business entity which hold the passed entityID.</summary>
        /// <exception cref="System.ArgumentException">Throw when the parameter is 0.</exception>
        public virtual T Get(int entityID)
        {
            if (entityID == 0)
                throw new ArgumentException("entityID");

            return repository.Get(entityID);
        }

        /// <summary>Return all business entity from the database.</summary>
        /// <returns></returns>
        public virtual IList<T> Gets()
        {
            return repository.Gets();
        }

        /// <exception cref="System.ArgumentException">Throw when the parameter is 0.</exception>
        public virtual IList<T> Gets(Specification<T> specs)
        {
            if (specs == null)
                throw new ArgumentNullException("specs");

            return repository.Gets(specs);
        }
    }
}
